<template>
  <div>
    <SearchContainer @search="getData()" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="药品名称/药品编号/助记码  " />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-end">
        <BaseExport type="药库" name="药品库存查询" code="000309" :search-params="formState" class="mb-2">导出</BaseExport>
      </div>
      <BaseTable
        :data-source="tableData"
        fill
        :loading="loading"
        :scroll="{ x: 1800 }"
        column-code="000309"
        :pagination="pagination"
        @change="onPageChange"
        @expand="onShowChild"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" :disabled="record.status" @click="onDetail(record)">出入库明细</a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Detail v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import useUserStore from '@/store/user'
import Detail from './Detail'

const { empId } = useUserStore()
const route = useRoute()
const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const formState = ref({
  zjm: '',
  ckbh: []
})

const onSuccess = () => {
  getData()
}
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getStoreSummaryByDrug',
  immediate: false,
  formatParams: () => {
    return {
      ...formState.value,
      materialType: route.query?.routerType || undefined // 防疫物资
    }
  }
})
const getDepartInfo = async () => {
  const res = await post('/common/masterData/getByYsbhDepartment', { empId: empId })
  if (res.code !== 0) message.error(res.msg || '获取登录人科室失败')
  formState.value.ckbh = ['DEPTCODE' + res.data?.ksdm]
  getData()
}

const onDetail = (record) => {
  editOpen.value = true
  rowData.value = record
}
onMounted(() => {
  getDepartInfo()
})
</script>
